package com.office.automate.dataaccess.impl.administration;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.office.automate.dataaccess.def.administration.SecurityLevelDao;
import com.office.automate.dataaccess.framework.DataAccessSessionFactory;
import com.office.automate.exception.administration.SecurityLevelExceptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.administration.SecurityLevel;

public class SecurityLevelDaoImplementer implements SecurityLevelDao {

	private static SessionFactory sessionFactory = null;
	private Session session = null;

	static {
		sessionFactory = DataAccessSessionFactory.getSessionFactory();
	}

	public SecurityLevelDaoImplementer() {
		session = sessionFactory.openSession();		
	}

	public void persistSecurityLevelDetails(SecurityLevel aSecurityLevel) throws FrontierVisibleException {
		try {
			Transaction transaction = session.beginTransaction();
			session.save(aSecurityLevel);
			transaction.commit();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.FAIL_TO_PERSIST_SECURITY_LEVEL, e);
		} finally {
			session.flush();
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<SecurityLevel> getAvailableSecurityLevels() throws FrontierVisibleException {
		List<SecurityLevel> availableSecurityLevels = null;
		try {
			 String SQL_QUERY ="from SecurityLevel securitylevel";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 availableSecurityLevels = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return availableSecurityLevels;
	}

	@SuppressWarnings("unchecked")
	public List<SecurityLevel> getAvailableSecurityLevelsPage(int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<SecurityLevel> availableSecurityLevels = null;
		try {
			 String SQL_QUERY ="from SecurityLevel securitylevel order by securitylevel.securityLevelId asc";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 queryResult.setFirstResult(aStartingIndex);
			 queryResult.setMaxResults(aNumberOfRecords);
			 availableSecurityLevels = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return availableSecurityLevels;
	}

	public void deleteSecurityLevel(SecurityLevel aSecurityLevel) throws FrontierVisibleException {
		try {
			session.beginTransaction();   
			session.delete(aSecurityLevel);  
			session.getTransaction().commit();  
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_DELETION, e);
		} finally {
			session.flush();
			session.close();
		}
	}

	public int getAvailableSecurityLevelRecordCount() throws FrontierVisibleException {
		int recordCount = 0;
		try {
			 String SQL_QUERY ="select count(*) from SecurityLevel securitylevel";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 recordCount = ((Long) queryResult.list().get(0)).intValue();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		}
		return recordCount;
	}
}